package com.appspot.battlerafts.servlets.lobby;

import com.appspot.battlerafts.classes.User;
import com.appspot.battlerafts.jsons.ListOfUsersJSON;
import com.appspot.battlerafts.utils.PMF;
import com.google.gson.Gson;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created with IntelliJ IDEA.
 * User: Tonis
 * Date: 02.04.12
 * Time: 14:05
 * To change this template use File | Settings | File Templates.
 */
public class GetHighScore extends HttpServlet {

    private static final Logger log = Logger.getLogger(GetHighScore.class.getName());
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        Query q = pm.newQuery(User.class);
        q.setRange(0, 10);
        q.setOrdering("score DESC");
        List<User> bestScoringUsers = (List<User>) q.execute();
        log.info("BEST SCORING: "+ bestScoringUsers);
        log.setLevel(Level.INFO);
        ListOfUsersJSON listOfUsersJSON = new ListOfUsersJSON();
        listOfUsersJSON.userList = bestScoringUsers;

        Gson gson = new Gson();
        response.setContentType("application/json");
        PrintWriter writer = response.getWriter();
        writer.write(gson.toJson(listOfUsersJSON));
    }
}
